import 'package:camera/camera.dart';
import 'package:logging/logging.dart';

/// 相机服务
/// 负责管理相机的初始化、配置和控制
/// 创建时间：2024-01-23

class CameraService {
  final _logger = Logger('CameraService');
  CameraController? controller;
  List<CameraDescription> cameras = [];

  Future<void> initializeCamera() async {
    try {
      cameras = await availableCameras();
      if (cameras.isEmpty) {
        _logger.severe('没有可用的相机');
        return;
      }

      controller = CameraController(
        cameras[0],
        ResolutionPreset.medium,
        enableAudio: false,
      );

      await controller?.initialize();
      _logger.info('相机初始化成功');
    } catch (e) {
      _logger.severe('相机初始化失败: $e');
    }
  }

  void dispose() {
    controller?.dispose();
  }
}
